home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 41 (1994-09)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip
/
MegaDisc 41 (1994-09)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf
/
Programming
/
PASCAL_TUTES
/
tute5.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1994-06-25
|
14KB
|
371 lines
{
Tutorial Five
Real numbers don't eat quiche...
Get a cup of coffee and take a deep breath, this one's going to
use at least three brain cells...
In Tute2 we met variables for the first time. So far we've seen
integer and string variables. Are there others? Yessir! Below
are some simple variable types...
------------------------------
INTEGER: Whole numbers. There are several sub-categories of
integer, and they have the following definitions for
HiSpeed Pascal...
ShortInt = -128..127;
Byte = 0..255;
Integer = -32768..32767;
Word = 0..65535;
LongInt = -2147483648..2147483647;
Variables of the type ShortInt and Byte use one byte of memory,
Integers and Words use 2 bytes, and LongInts use 4 bytes. Choosing
the correct variable type can make your program faster! Which
integer type would be the most suitable for the variable "yourage"
seen in tute2? Perhaps you'd like to go back and re-write that
program with a better variable type. Or perhaps you'd rather
rassle a rooster? Who knows?
------------------------------
CHAR: 256 single characters make up the definition of the variable
type Char. These are usually the ASCII set of characters,
and they comprise the following...
| 0 ^@ | 32 | 64 @ | 96 ` | 128 . | 160 | 192 À | 224 à |
| 1 ^A | 33 ! | 65 A | 97 a | 129 . | 161 ¡ | 193 Á | 225 á |
| 2 ^B | 34 " | 66 B | 98 b | 130 . | 162 ¢ | 194 Â | 226 â |
| 3 ^C | 35 # | 67 C | 99 c | 131 . | 163 £ | 195 Ã | 227 ã |
| 4 ^D | 36 $ | 68 D | 100 d | 132 . | 164 ¤ | 196 Ä | 228 ä |
| 5 ^E | 37 % | 69 E | 101 e | 133 . | 165 ¥ | 197 Å | 229 å |
| 6 ^F | 38 & | 70 F | 102 f | 134 . | 166 ¦ | 198 Æ | 230 æ |
| 7 ^G | 39 ' | 71 G | 103 g | 135 . | 167 § | 199 Ç | 231 ç |
| 8 ^H | 40 ( | 72 H | 104 h | 136 . | 168 ¨ | 200 È | 232 è |
| 9 ^I | 41 ) | 73 I | 105 i | 137 . | 169 © | 201 É | 233 é |
| 10 ^J | 42 * | 74 J | 106 j | 138 . | 170 ª | 202 Ê | 234 ê |
| 11 ^K | 43 + | 75 K | 107 k | 139 . | 171 « | 203 Ë | 235 ë |
| 12 ^L | 44 , | 76 L | 108 l | 140 . | 172 ¬ | 204 Ì | 236 ì |
| 13 ^M | 45 - | 77 M | 109 m | 141 . | 173 | 205 Í | 237 í |
| 14 ^N | 46 . | 78 N | 110 n | 142 . | 174 ® | 206 Î | 238 î |
| 15 ^O | 47 / | 79 O | 111 o | 143 . | 175 ¯ | 207 Ï | 239 ï |
| 16 ^P | 48 0 | 80 P | 112 p | 144 . | 176 ° | 208 Ð | 240 ð |
| 17 ^Q | 49 1 | 81 Q | 113 q | 145 . | 177 ± | 209 Ñ | 241 ñ |
| 18 ^R | 50 2 | 82 R | 114 r | 146 . | 178 ² | 210 Ò | 242 ò |
| 19 ^S | 51 3 | 83 S | 115 s | 147 . | 179 ³ | 211 Ó | 243 ó |
| 20 ^T | 52 4 | 84 T | 116 t | 148 . | 180 ´ | 212 Ô | 244 ô |
| 21 ^U | 53 5 | 85 U | 117 u | 149 . | 181 µ | 213 Õ | 245 õ |
| 22 ^V | 54 6 | 86 V | 118 v | 150 . | 182 ¶ | 214 Ö | 246 ö |
| 23 ^W | 55 7 | 87 W | 119 w | 151 . | 183 · | 215 × | 247 ÷ |
| 24 ^X | 56 8 | 88 X | 120 x | 152 . | 184 ¸ | 216 Ø | 248 ø |
| 25 ^Y | 57 9 | 89 Y | 121 y | 153 . | 185 ¹ | 217 Ù | 249 ù |
| 26 ^Z | 58 : | 90 Z | 122 z | 154 . | 186 º | 218 Ú | 250 ú |
| 27 ^[ | 59 ; | 91 [ | 123 | 155 . | 187 » | 219 Û | 251 û |
| 28 ^\ | 60 < | 92 \ | 124 | | 156 . | 188 ¼ | 220 Ü | 252 ü |
| 29 ^] | 61 = | 93 ] | 125 | 157 . | 189 ½ | 221 Ý | 253 ý |
| 30 ^^ | 62 > | 94 ^ | 126 ~ | 158 . | 190 ¾ | 222 Þ | 254 þ |
| 31 ^_ | 63 ? | 95 _ | 127 | 159 . | 191 ¿ | 223 ß | 255 ÿ |
After character 127 the characters do vary depending on which
accented national characters are being used. I left out 123 and
125 because they are the opening and closing curly bracket
characters! That would really mess up our comments,and
possibly our hair as well...
------------------------------
STRING: A sequence of characters with a dynamic length within a
fixed maximum length. The length can range from zero to
255. Strings may be compared like numbers. We met
strings before and we'll meet them again.
------------------------------
REAL: A real number has fractional parts, and a much greater
range than the integer types. For example...
Real Integer
---- -------
4.500 4
-5 -5
1.2e3 1200
The "e" indicates exponent to the base 10, so that 1.2e3 is the
same as 1.2 x 10³, which is 1.2 x 1000 or 1200. Clear as mud!
HiSpeed Pascal supports the following REAL type definitions...
Single = 3.4e-38..3.4e38
Double = 1.7e-308..1.7e308 (This is the same as typing "Real")
Extended = 1.1e-4932..1.1e4932
Remember that these ranges are positive as well as negative, so if
you ever have a need for a negative number with 4932 zeros after
it, you know where to go (a psychiatrist?).
------------------------------
There are some other simple variable types, but we'll go into them
in another tutorial. Right now, I'd like to use the variable type
REAL in a program which does some simple calculations. Let's make
a program that takes the radius of a circle from the user and
calculates the diameter, area and circumference of that circle. If
you're a bit of a maths quamby, take a look at Diagram_two for a
refresher course!
}
Program MDTute5 (input,output);
{ This program uses "real" numbers to do some simple
mathematical calculations with circles
Author : A N Peck
Date : 19 June 1994 }
const
{
^
|
|__
This is another reserved word that allows you to put some
constants in at the start of the program. The advantage is
two-fold. Firstly, everytime you need the constant in the
program, you can refer to your previous definition. Secondly,
if you want to change the constant, you need only alter it at
the start of the program, and it will be substituted in all
the way through (useful for really long programs).
}
pi = 3.14159;
{
^ ^ ^ ^
| | | |
| | | |__
A semi-colon to end each constant definition!
| | |
| | |__
After the equals sign comes the actual constant. Now,
| |
whenever the compiler sees "pi" in the program, it will
| |
substitute 3.14159. If we need to change this later to
| |
add precision, we only need to change the constant
| |
definition. Bogus!
| |
| |__
We let the compiler know what the constant is with an
|
"equals" sign.
|
|__
The constant name comes first, in this instance we are
defining the number that the word "pi" will represent.
}
tab = ' ';
{
^
|
|__
A constant doesn't have to be a number. Here we've defined
that the word "tab" will be equal to five blank spaces. We
can then use the "tab" for text formatting.
}
var
radius,diameter,circumference,area: real;
{
^ ^
| |
| |__
The variable type, in
|
this case "real".
|
We could have used
|
"single","double" or
|
"extended". Now the user
|
can input numbers with
|
decimal points, and be
|
given answers to that same
|
precision!
|
|__
Because all the variables are of the same type, we can list
them together like this, separating them with commas. It
would be just as accurate to list them like...
var
radius: real;
diameter: real;
etc.,...
...but why make work for ourselves?
}
begin
writeln;
{
^
|
|__
We'll start with a blank line to make the output look pretty!
This is called programming as a fashion statement...
}
write(tab,'Please enter the radius of your circle: ');
{
^
|
|__
Here's our first constant popping up! When the
compiler sees this word "tab" it will insert the five
blank spaces that we have defined above. Cool!
}
readln(radius);
diameter := radius*2;
{
^ ^
| |
| |__
We use this to indicate multiplication.
|
Some of the mathematical operations
|
available are represented as follows...
|
|
Addition: + e.g. 3 + 4 = 7
|
|
Subtraction: - e.g. 10 - 2 = 8
|
|
Multiplication: * e.g. 4.3 * 2 = 8.6
|
|
Division: / e.g. 8 / 4 = 2
|
|__
Yikes! What is this? This sign is the same as
saying "is equal to", but a bit more forceful! We
are telling the compiler that the variable "diameter"
WILL be made equal to the "radius" variable
multiplied by two. Note how it differs from the
normal equals sign used for constant declarations.
We'll see other equals signs in another tutorial
(gulp!).
}
circumference := diameter*pi;
{
^
|
|__
Here's the other constant!
}
area := pi*radius*radius;
{
^
|
|__
There are different ways to approach this. The
formula for area of a circle is A = pi x r². Instead
of writing the r² bit as radius*radius, pascal has an
inbuilt squaring function called SQR. So we could
have written it as...
area := pi*SQR(radius);
I'm just lazy and also a strong believer in the
K.I.S.S. principle (Keep It Simple Stupid).
}
writeln;
writeln(tab,'Dimensions for a circle of radius',radius:0:2,':');
{
^ ^
| |
Real variables give their full precision,
__| |
but often we only need the answer to two
|
decimal places. This first colon tells the
|
the compiler that some formatting information
|
is about to follow. The zero indicates that
|
we want zero places between the last thing
|
written, and the start of the number. We'll
|
see how useful this is when formatting
|
columns in a later tutorial.
|
|
The second colon indicates that a number
__|
representing the precision is about to follow,
in this instance we are requesting that the
variable "radius" be shown to just two
decimal places. Radical!
This can be a confusing point so I'll elaborate. Let's say that
we have a variable "radius" and the user gives us the value 4.567.
Here are some of the possible formats we can use...
writeln(radius) gives 4.567000000000000E+000
writeln(radius:10:0) gives 5
writeln(radius:8:2) gives 4.57
writeln(radius:2:3) gives 4.567
writeln(radius:0:5) gives 4.56700
Notice how the first one, with no input from us on formatting, is
totally out of control! The second places the number 10 places in
from the edge, but allows no decimal points. In fact, it rounds
off the number given as well (bonus!). Also note the extra
precision given to the last format. Awesome!
The first number is left formatting so the command
writeln(radius:2:3) is no different than writeln(radius:0:3).
You'll see this more clearly when we come to columns!
Note that even though I don't ask for a space (writeln(radius:0:2)),
I've left room for one by not putting in a blank after (...radius',).
This is because when dealing with real numbers, you need to take
into account the sign! Although not shown for positive numbers, an
answer like 56.75 is really +56.75. If you're not sure on this, run
the program with a negative radius.
Have a play with this formatting until you either understand it,
or feel like ripping your own head off.
}
writeln;
writeln(tab,'Diameter:',diameter:0:2);
{
^
|
|__
We can format the resultant
answer given to us by the
calculation we defined above.
All of these answers will have
a precision of two decimal
points.
}
writeln(tab,'Circumference:',circumference:0:2);
writeln(tab,'Area:',area:0:2);
writeln;
end.
{
Wow! Fantastic. What sort of debugging should we do on a program
like this? Try some of the following...
* Enter the radius as 0, 0.0, 00.00, etc.,.
* Try a negative radius (twilight zone circles).
* When asked for the radius, type in your name!
* Enter a radius with a precision greater than that displayed, for
example try 0.004
* Try a VERY big number, like 47385627847264...
* Can you enter in an exponent? Try 3.4e4, or 2e-1.
It's always a good idea to get another person to look at the
finished product because they're sure to find some flaws where you
see only perfection.
----------------------END OF TUTE5.PAS----------------------
}